package com.components.project.ui.fragment

import android.content.Intent
import android.os.Bundle
import android.widget.ProgressBar
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import com.android.toast.AndroidToast
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.components.base.layout.SettingBarLayout
import com.components.base.layout.SwitchButtonLayout
import com.components.base.titlebar.TitleBar
import com.components.project.MainActivity
import com.components.project.R
import com.components.project.ui.activity.TitleBarActivity
import com.components.project.ui.base.ProjectBaseFragment
import com.components.update.callback.DownloadListener
import com.components.update.http.DownloadManager
import com.components.update.util.NotificationForProgressUtils
import com.components.zxing.ConstantManage
import com.components.zxing.ui.ScanCodeActivity
import java.io.File

class UserCenterFragment : ProjectBaseFragment<MainActivity>() {

    private var titleBar: TitleBar? = null
    private var switchButton: SwitchButtonLayout? = null
    private var progressBar: ProgressBar? = null
    private var url =
        "https://imagecdntest.cqliving.com/android/app_4/NA_20220601_V6.2.2_6022_ready_1.apk"
    private var scanActivityResult: ActivityResultLauncher<Intent>? = null

    override fun getLayoutId(): Int {
        return R.layout.fragment_user_center
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //这个必须在最开始创建，不然会崩溃
        scanActivityResult =
            registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
                ToastUtils.showShort(it.data?.getStringExtra(ConstantManage.CODE_CONTENT))
            }
    }

    override fun initView() {
        titleBar = findViewById(R.id.titleBar)
        switchButton = findViewById(R.id.switchButton)
        switchButton?.setOnCheckedChangeListener(object :
            SwitchButtonLayout.OnCheckedChangeListener {
            override fun onCheckedChanged(button: SwitchButtonLayout?, isChecked: Boolean) {
                ToastUtils.showShort(isChecked.toString())
            }
        })
        progressBar = findViewById(R.id.progressBar)
        findViewById<SettingBarLayout>(R.id.updateLayout).setOnClickListener {
            val downloadManager: DownloadManager = DownloadManager()
            downloadManager.add(ActivityUtils.getTopActivity(), url, object : DownloadListener {
                override fun onFinished(file: File) {
                    LogUtils.e(file)
                }

                override fun onProgress(progress: Float) {
                    NotificationForProgressUtils.createNotificationForProgressUtils((progress * 100).toInt(),100)
                    progressBar?.progress = (progress * 100).toInt()
                }

                override fun onPause() {

                }

                override fun onCancel() {

                }

            })
            downloadManager.download(url)
        }

        findViewById<SettingBarLayout>(R.id.exampleLayout).setOnClickListener {
            AndroidToast(ActivityUtils.getTopActivity())
                .setDuration(2000)
                .setContentView(R.layout.window_hint)
                .setAnimStyle(R.style.IOSAnimStyle)
                .setImageDrawable(android.R.id.icon, R.mipmap.ic_dialog_tip_error)
                .setText(android.R.id.message, "我会自己消失")
                .show()
        }

        findViewById<SettingBarLayout>(R.id.titleBarSettingBarLayout).setOnClickListener {
            startActivity(Intent(context, TitleBarActivity::class.java))
        }

        findViewById<SettingBarLayout>(R.id.titleBarScan).setOnClickListener {
            scanActivityResult?.launch(Intent(context, ScanCodeActivity::class.java))
        }
    }

    override fun initData() {

    }

    override fun onResume() {
        super.onResume()
        getStatusBarConfig()?.statusBarDarkFont(true)?.init()
    }

}